home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / c1.arc / LIST1.C < prev   
Text File  |  1984-12-26  |  6KB  |  341 lines

  1. /*
  2.  
  3.  
  4.              ┌──────────────────────────────────┐
  5.              │ File Name: ██     list1.c    ██    │
  6.              └──────────────────────────────────┘
  7.  
  8.  
  9.         The following C source is taken from the TELECON SYSTEMS
  10.     standard I/O library. Permission is hereby granted for personal,
  11.     non-comercial use of this code or any derivitives.
  12.  
  13.       Copyright (c) 1981,  TELECON SYSTEMS. All rights reserved.
  14.  
  15.            COMMON portion of the STANDARD I/O LIBRARY.
  16.  
  17.                     27-jan-84
  18.  
  19.     Table of content:
  20.  
  21.         - printf()
  22.         - fprintf()
  23.         - sprintf()
  24.  
  25.         - strlen()
  26. */
  27.  
  28. #define     CONSOLE       0
  29. #define     CODE         -1
  30. #define     DEVICE          0
  31. #define     MEMORY          1
  32.  
  33. #define     linesize    133
  34. #define     linemax     132
  35.  
  36.     static    char    *obptr,outbfr[40];
  37.     static    int    field,fldskip,leftjust,longflag,padchar,precision,
  38.             pushback = 0;
  39.         char    line[linesize],*lineptr,
  40.             *stderr = CONSOLE,*stdin = CONSOLE,*stdout = CONSOLE;
  41.         int    ag1fl = DEVICE,echo = 1,enablout = 1,uconly = 0;
  42.  
  43.     typedef  unsigned  FILE;
  44. #define     EOF        -1
  45. #define     NULL        0
  46. /*  Macros.    */
  47.  
  48. #define     islower(c)    (c >= 'a' && c <= 'z')
  49. #define     toupper(c)    (c &= 0x5F)
  50. #define     isdigit(c)    (c >= '0' && c <= '9')
  51.  
  52. /* printf - format the input stream and output it to the
  53.    "standard" output.   */
  54.  
  55. printf(fmtstr,list)
  56.     char        *fmtstr;
  57.     unsigned    list;
  58.     {
  59.  
  60.     outf(fmtstr,&list);
  61.     }
  62.  
  63. /* fprintf - format the input stream and output it to the
  64.    specified file. */
  65.  
  66. fprintf(file,fmtstr,list)
  67.     char        *file,*fmtstr;
  68.     unsigned    list;
  69.     {
  70.     FILE    *temp;
  71.  
  72.     /* Save the current reference in 'stdout' and substitute
  73.        the specified file reference. */
  74.  
  75.     temp = stdout;
  76.     stdout = file;
  77.  
  78.     outf(fmtstr,&list);
  79.  
  80.     /* Now restore the original reference in 'stdout'. */
  81.  
  82.     stdout = temp;
  83.     }
  84.  
  85. /* sprintf - format the input stream and output it to the
  86.    specified buffer. */
  87.  
  88. sprintf(buffer,fmtstr,list)
  89.     char        *buffer,*fmtstr;
  90.     unsigned    list;
  91.     {
  92.     FILE    *temp;
  93.     int    flag;
  94.  
  95.     /* Save the current reference in 'stdout' and substitute
  96.        the buffer address. */
  97.  
  98.     temp = stdout;
  99.     stdout = buffer;
  100.     flag = ag1fl;
  101.     ag1fl = MEMORY;
  102.  
  103.     outf(fmtstr,&list);
  104.  
  105.     /* Now restore the original reference in 'stdout'. */
  106.  
  107.     stdout = temp;
  108.     ag1fl = flag;
  109.     }
  110.  
  111. /* outf - common formatting and output function for "printf", "fprintf"
  112.    and "sprintf". */
  113.  
  114. outf(fmtstr,list)
  115.     char        *fmtstr;
  116.     unsigned    *list;
  117.     {
  118.  
  119.     int    c,j,k;
  120.  
  121.     while(c = *fmtstr++)
  122.         {
  123.         if(c != '%') { putchar(c); continue; }
  124.         obptr = outbfr;
  125.         field = leftjust = longflag = precision = 0;
  126.         padchar = ' ';
  127.  
  128.         while(c = *fmtstr++)
  129.             {
  130.             if(c == '-') leftjust = 1;
  131.             else if(c == '0') padchar = '0';
  132.             else if(isdigit(c))
  133.                 { --fmtstr; field = atoint(&fmtstr); }
  134.             else if(c == '.') precision = atoint(&fmtstr);
  135.             else if((c |= 0x20) == 'l') longflag = 1;
  136.             else
  137.                 {
  138.                 switch(c)
  139.                 {
  140.                 case 'c':
  141.                     obstore(*list++);
  142.                     bfrout(outbfr);
  143.                     break;
  144.  
  145.                 case 'd':
  146.                     if(longflag)
  147.                         outfixed((long *) *list++,10);
  148.                     else
  149.                         outfixed((long) *list++,10);
  150.                     bfrout(outbfr);
  151.                     break;
  152.  
  153.                 case 'e':
  154.                 case 'g':
  155.                     outefloat((float *) *list++);
  156.                     bfrout(outbfr);
  157.                     break;
  158.                 case 'f':
  159.                     outffloat((float *) *list++);
  160.                     bfrout(outbfr);
  161.                     break;
  162.  
  163.                 case 'o':
  164.                     if(longflag)
  165.                         outfixed((long *) *list++,8);
  166.                     else    outfixed(0,*list++,8);
  167.                     bfrout(outbfr);
  168.                     break;
  169.  
  170.                 case 's':
  171.                     bfrout(*list++);
  172.                     break;
  173.  
  174.                 case 'x':
  175.                     if(longflag)
  176.                         outfixed((long *) *list++,16);
  177.                     else    outfixed(0,*list++,16);
  178.                     bfrout(outbfr);
  179.                     break;
  180.                 default:
  181.                     putchar(c);
  182.                 }
  183.                 break;
  184.                 }
  185.             }
  186.         }
  187.     }
  188.  
  189. /* Subroutines used by outf()    */
  190.  
  191. outfixed(value,base)
  192.     long    value;
  193.     int    base;
  194.     {
  195.     long    i;
  196.  
  197.     if(base == 10)
  198.         {
  199.         if(value < 0)
  200.             {
  201.             obstore('-');
  202.             value = -value;
  203.             }
  204.         }
  205.     switch(base)
  206.         {
  207.         case 8:     i = value >> 3;
  208.                 break;
  209.         case 10:    if((i = value / 10) < 0) i = -i;
  210.                 break;
  211.         case 16:    i = value >> 4;
  212.                 break;
  213.         }
  214.     if(i) outfixed(i,base);
  215.  
  216.     switch(base)
  217.         {
  218.         case 8:     value &= 7;
  219.                 break;
  220.         case 10:    if((value %= 10) < 0) value = -value;
  221.                 break;
  222.         case 16:    value &= 0xF;
  223.                 break;
  224.         }
  225.     if(value > 9) value += 7;
  226.     obstore((int) value + '0');
  227.     }
  228.  
  229. /* 'e' format, floating point output conversion. */
  230.  
  231. outefloat(fv)
  232.     float    fv;
  233.     {
  234.     int    exponent,sign;
  235.  
  236.     exponent = sign = 0;
  237.  
  238.     if(fv < 0.0) { sign = 1; fv = -fv; }
  239.  
  240.     while(1)
  241.         {
  242.         if(fv < 1.0)        fv *= 10.0, --exponent;
  243.         else if(fv >= 10.0)    fv /= 10.0, ++exponent;
  244.         else break;
  245.         }
  246.     if(sign) fv = -fv;
  247.     outffloat(fv);
  248.     obstore('E');
  249.     outfixed((long) exponent,10);
  250.     }
  251.  
  252. /* 'f' format, floating point output conversion. */
  253.  
  254. outffloat(fv)
  255.     float    fv;
  256.     {
  257.     long    fixed;
  258.  
  259.     if(!precision)    precision = 6;
  260.     if(fv < 0.0) { fv = -fv; obstore('-'); }
  261.     outfixed(fixed = (long)fv,10);
  262.     obstore('.');
  263.  
  264.     while(precision)
  265.         {
  266.         fv -= (float)fixed;
  267.         fv *= 10.0;
  268.         fixed = (long)fv;
  269.         obstore((int)fixed + '0');
  270.         --precision;
  271.         }
  272.     }
  273.  
  274. /* Output buffer, store routine. */
  275.  
  276. obstore(ascii)
  277.     int    ascii;
  278.     {
  279.     *obptr++ = ascii;
  280.     }
  281.  
  282. /* Buffer output routine. */
  283.  
  284. bfrout(outbfr)
  285.     char    *outbfr;
  286.     {
  287.     int    c,length;
  288.  
  289.     *obptr = NULL;
  290.     length = strlen(outbfr);
  291.  
  292.     if(!precision) precision = length;
  293.  
  294.     length = field - precision;
  295.     if(!leftjust) padout(length);
  296.     while((c = *outbfr++) && precision--) putchar(c);
  297.     if(leftjust) padout(length);
  298.     }
  299.  
  300. /* Output field, pad routine. */
  301.  
  302. padout(length)
  303.     int    length;
  304.     {
  305.     if(length < 0) return;
  306.     while(length--) putchar(padchar);
  307.     }
  308.  
  309. int    strlen(str1)
  310.     char    str1[];
  311.     {
  312.     int    j;
  313.  
  314.     j = 0;
  315.     while(str1[j]) ++j;
  316.     return j;
  317.     }
  318.  
  319. putchar(c)
  320.     char    c;
  321.     {
  322.  
  323.     if(enablout)
  324.         {
  325.         if(c == '\n') putchar('\r');
  326.         if(uconly)
  327.             if(islower(c)) toupper(c);
  328.         if(stdout)
  329.             {
  330.             if(ag1fl == MEMORY) *stdout++ = c;
  331.             else if((putc(c,stdout)) == EOF)
  332.                 {
  333.                 fclose(stdout);
  334.                 stdout = NULL;
  335.                 }
  336.             }
  337.         else (putach(c));
  338.         }
  339.     return c;
  340.     }
  341.